package com.micro;

import com.micro.data.AccountInfo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		GridView gridView = (GridView) findViewById(R.id.category);
		gridView.setAdapter(new ImageAdapter());
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch ((int) id) {
				case R.drawable.sina_logo:
					AccountInfo.setContext(MainActivity.this);
					AccountInfo account = AccountInfo.loadAccount();
					if (account == null) {
						startActivity(new Intent(MainActivity.this,
								AddAcountActivity.class));
					} else {
						startActivity(new Intent(MainActivity.this,
								HomeActivity.class));
					}
					break;

				default:
					break;
				}
			}
		});
	}

	public class ImageAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return logos.length;
		}

		@Override
		public Object getItem(int position) {
			return logos[position];
		}

		@Override
		public long getItemId(int position) {
			return logos[position];
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = new ImageView(MainActivity.this);
			}
			((ImageView) convertView).setImageResource(logos[position]);
			((ImageView) convertView).setScaleType(ImageView.ScaleType.FIT_XY);
			((ImageView) convertView)
					.setLayoutParams(new GridView.LayoutParams(80, 80));
			return convertView;
		}
	}

	private Integer[] logos = new Integer[] { R.drawable.sina_logo,
			R.drawable.qq_logo };
}